//
//  SegmentView.swift
//  手账
//
//  Created by zhuxiaoxiang on 2018/12/17.
//  Copyright © 2018年 zhuxiaoxiang. All rights reserved.
//

import UIKit

private let kSelectedColor = UIColor.hex(hexString: "3C87FE")
private let kDisabledColor = UIColor.hex(hexString: "333333")

class SegmentView: UIView {
    
    typealias SelectBlock = ((_ moneyType: MoneyType) -> ())
    
    enum MoneyType {
        case income
        case expend
        
        var typeString: String {
            get {
                switch self {
                case .income:
                    return "收入"
                default:
                    return "支出"
                }
            }
        }
    }
    
    var moneyType: MoneyType = .income
    var _block: SelectBlock?
    
    lazy var incomeBtn: UIButton = {
        let one = UIButton(type: .custom)
        one.frame = CGRect(x: 0, y: 5, width: self.frame.size.width / 2, height: self.frame.size.height - 10)
        one.setTitle("收入", for: .normal)
        one.tag = 1000
        one.setTitleColor(kSelectedColor, for: .normal)
        one.titleLabel?.font = UIFont.init(name: "PingFangSC-Regular", size: 18)
        one.addTarget(self, action: .changeType, for: .touchUpInside)
        return one
    }()
    
    lazy var expendBtn: UIButton = {
        let one = UIButton(type: .custom)
        one.frame = CGRect(x: self.frame.size.width / 2, y: 5, width: self.frame.size.width / 2, height: self.frame.size.height - 10)
        one.setTitle("支出", for: .normal)
        one.tag = 1001
        one.setTitleColor(UIColor.black, for: .normal)
        one.titleLabel?.font = UIFont.init(name: "PingFangSC-Regular", size: 18)
        one.addTarget(self, action: .changeType, for: .touchUpInside)
        return one
    }()
    
    fileprivate lazy var flagView: UIView = {
        let one = UIView(frame: CGRect(x: 22, y: self.frame.size.height - 2, width: 36.3333, height: 2))
        one.backgroundColor = kSelectedColor
        return one
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(self.incomeBtn)
        self.addSubview(self.expendBtn)
        self.addSubview(self.flagView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension SegmentView {
    @objc func changeType(button: UIButton) {
        let tag = button.tag
        switch tag {
        case 1000:
            // 收入
            if self.moneyType == .income {
                return
            }
            self.moneyType = .income
            self.incomeBtn.setTitleColor(kSelectedColor, for: .normal)
            self.expendBtn.setTitleColor(kDisabledColor, for: .normal)
            UIView.animate(withDuration: 0.1, animations: {
                self.flagView.frame = CGRect(x: 22, y: self.frame.size.height - 2, width: 36.3333, height: 2)
            })
        default:
            // 支出
            if self.moneyType == .expend {
                return
            }
            self.moneyType = .expend
            self.incomeBtn.setTitleColor(kDisabledColor, for: .normal)
            self.expendBtn.setTitleColor(kSelectedColor, for: .normal)
            UIView.animate(withDuration: 0.1, animations: {
                self.flagView.frame = CGRect(x: self.frame.size.width / 2 + 22, y: self.frame.size.height - 2, width: 36.3333, height: 2)
            })
            break
        }
        guard let block = _block else {
            return
        }
        block(self.moneyType)
    }
}

private extension Selector {
    static let changeType = #selector(SegmentView.changeType)
}
